import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class TicketDaoImpl implements TicketDao {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private ShowDao showDao = new ShowDaoImpl();

    @Override
    public boolean addTicket(Ticket ticket) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO ticket VALUES (?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, ticket.getTicketId());
            pstmt.setString(2, ticket.getShow().getShowId());
            pstmt.setInt(3, ticket.getSeatRow());
            pstmt.setInt(4, ticket.getSeatCol());
            pstmt.setString(5, ticket.getCreateTime().format(formatter));
            pstmt.setBoolean(6, ticket.isRefunded());
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }

    @Override
    public boolean updateTicketRefundStatus(String ticketId, boolean isRefunded) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE ticket SET is_refunded = ? WHERE ticket_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setBoolean(1, isRefunded);
            pstmt.setString(2, ticketId);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.closeResources(conn, pstmt, null);
        }
    }

    @Override
    public Ticket getTicketById(String ticketId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM ticket WHERE ticket_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, ticketId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                String showId = rs.getString("show_id");
                int seatRow = rs.getInt("seat_row");
                int seatCol = rs.getInt("seat_col");
                LocalDateTime createTime = LocalDateTime.parse(rs.getString("create_time"), formatter);
                boolean isRefunded = rs.getBoolean("is_refunded");

                Show show = showDao.getShowById(showId);
                if (show != null) {
                    Ticket ticket = new Ticket(ticketId, show, seatRow, seatCol);
                    ticket.setRefunded(isRefunded);
                    return ticket;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return null;
    }

    @Override
    public List<Ticket> getTicketsByShowId(String showId) {
        List<Ticket> tickets = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM ticket WHERE show_id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, showId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                String ticketId = rs.getString("ticket_id");
                int seatRow = rs.getInt("seat_row");
                int seatCol = rs.getInt("seat_col");
                boolean isRefunded = rs.getBoolean("is_refunded");

                Show show = showDao.getShowById(showId);
                if (show != null) {
                    Ticket ticket = new Ticket(ticketId, show, seatRow, seatCol);
                    ticket.setRefunded(isRefunded);
                    tickets.add(ticket);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeResources(conn, pstmt, rs);
        }
        return tickets;
    }
}
    